﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace AvalonDock
{
    /// <summary>
    /// Converter from boolean values to visibility (inverse mode)
    /// </summary>
    [ValueConversion(typeof(object), typeof(Visibility))]
    public class BoolToVisibilityConverter : IValueConverter
    {
        #region Public Methods and Operators

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return System.Convert.ToBoolean(value)
                       ? Visibility.Visible
                       : (parameter != null && ((string)parameter) == "Hidden"
                              ? Visibility.Hidden
                              : Visibility.Collapsed);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }

        #endregion
    }

    [ValueConversion(typeof(object), typeof(Image))]
    public class ObjectToImageConverter : IValueConverter
    {
        #region Public Methods and Operators

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double width = 16.0;
            if (parameter != null && parameter is double)
            {
                width = (double)parameter;
            }

            if (value is string)
            {
                Uri iconUri;
                // try to resolve given value as an absolute URI
                if (Uri.TryCreate(value as String, UriKind.RelativeOrAbsolute, out iconUri))
                {
                    var img = new BitmapImage(iconUri);
                    return new Image
                        {
#if NET4
                            UseLayoutRounding = true,
#endif
                            Width = width,
                            Source = img
                        };
                }
            }
            else if (value is BitmapImage)
            {
                var img = value as BitmapImage;
                return new Image
                    {
#if NET4
                        UseLayoutRounding = true,
#endif
                        Width = width,
                        Source = new BitmapImage(img.UriSource)
                    };
            }

            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public static class Converters
    {
        #region Constants and Fields

        private static BoolToVisibilityConverter _BoolToVisibilityConverter;

        private static ObjectToImageConverter _ObjectToImageConverter;

        #endregion

        #region Public Properties

        public static BoolToVisibilityConverter BoolToVisibilityConverter
        {
            get
            {
                return _BoolToVisibilityConverter ?? (_BoolToVisibilityConverter = new BoolToVisibilityConverter());
            }
        }

        public static ObjectToImageConverter ObjectToImageConverter
        {
            get
            {
                return _ObjectToImageConverter ?? (_ObjectToImageConverter = new ObjectToImageConverter());
            }
        }

        #endregion
    }
}